<template>
  <!-- 新增部门的弹层 -->
  <el-dialog title="新增部门" :visible="showDialog" @close="btnCancle">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-200个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择">
          <el-option v-for="employees in datarenyuan" :key="employees.id" :value="employees.username" :label="employees.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button type="primary" size="small" @click.native="addOk">确定</el-button>
        <el-button size="small" @click="btnCancle">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { adddepartment } from '@/api/deparment'
import { getemployeessimpleList } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      required: true
    },
    addChildrendepartment: {
      type: Object,
      required: true

    }

  },
  data() {
    return {
      formData: {
        code: '',
        introduce: '',
        manager: '',
        name: ''

      },
      datarenyuan: {},
      rules: {
        name: [
          { required: true, message: '必填项', trigger: 'blur' },
          { max: 50, message: '最大不能超过50个字节', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '必填项', trigger: 'blur' },
          { max: 200, message: '最大不能超过200个字节', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '必填项', trigger: 'blur' },
          { max: 300, message: '最大不能超过300个字节', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    this.init()
  },
  methods: {
    async addOk() {
      this.formData = {
        ...this.formData,
        pid: this.addChildrendepartment.id
      }
      // console.log(123)

      // console.log(this.formData)
      await adddepartment(this.formData)
      this.$message.success('添加部门成功')
      this.$emit('updata:showDialog', false)
    },
    async init() {
      this.datarenyuan = await getemployeessimpleList()
      // console.log(this.datarenyuan)
    },
    btnCancle() {
      this.$emit('updata:showDialog', false)
    }
  }
}
</script>
